package util;

import java.util.HashMap;
import java.util.Map;

import play.mvc.Action;
import play.mvc.Http.Context;
import play.mvc.Result;

public class EPTimer extends Action.Simple {

	private final static ThreadLocal<Map<String, Long>> map = new ThreadLocal<Map<String, Long>>() {

		protected Map<String, Long> initialValue() {
			return new HashMap<String, Long>();
		};

	};

	public static void start(String string) {
		map.get().put(string, System.currentTimeMillis());
	}

	public static void end(String string) {
		Long start = map.get().remove(string);

		Long end = System.currentTimeMillis();
		System.out.println(string + " delay: " + (end - start) + "milis");

	}

	@Override
	public Result call(Context ctx) throws Throwable {

		String ts = ctx.toString();
		start(ts);
		Result r = delegate.call(ctx);
		end(ts);
		return r;
	}
}
